package com.task.server.impl;

import com.task.mongo.ChatRecordDao;
import com.task.pojo.ChatContent;
import com.task.pojo.ChatDate;
import com.task.pojo.ChatRecord;
import com.task.server.ChatRecordService;
import com.task.util.DateUtil;
import com.task.util.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author jiafujun
 * 2020年03月26日 09:51:00
 */
@Service("ChatRecordService")
public class ChatRecordServiceImpl implements ChatRecordService {

    @Autowired
    private ChatRecordDao chatRecordDao;

    @Override
    public void save(ChatContent record) {
        String id = record.getSenderId().toString() + "and" + record.getRecipientId().toString();
        if (record.getSenderId() > record.getRecipientId()) {
            id = record.getRecipientId().toString() + "and" + record.getSenderId().toString();
        }
        id = MD5Util.MD5(id);
        ChatRecord query = new ChatRecord();
        query.setUserIds(id);
        ChatRecord chatRecord = chatRecordDao.queryOne(query);
        //有数据记录
        if (chatRecord != null) {
            List<ChatDate> chatDate = chatRecord.getChatDate();
            String rDate = DateUtil.getStrDate(record.getDateTime());
            boolean isnull = true;
            if (chatDate == null) {
                chatDate = new ArrayList<>();
            }
            //当天有记录
            for (ChatDate data : chatDate) {
                if (rDate.equals(DateUtil.getStrDate(data.getRecordDate()))) {
                    isnull = false;
                    data.getChatContent().add(record);
                    break;
                }
            }
            //当天无记录
            if (isnull) {
                List<ChatContent> contents = new ArrayList<>();
                contents.add(record);
                ChatDate date = new ChatDate();
                date.setRecordDate(DateUtil.getDateForStr(rDate));
                date.setChatContent(contents);
                chatDate.add(date);
            }
        } else {
            chatRecord = new ChatRecord();
            chatRecord.setUserIds(id);
            List<ChatContent> contents = new ArrayList<>();
            contents.add(record);
            ChatDate chatDate = new ChatDate();
            chatDate.setRecordDate(DateUtil.getDateForStr(DateUtil.getStrDate(record.getDateTime())));
            chatDate.setChatContent(contents);
            List<ChatDate> chatDates = new ArrayList<>();
            chatDates.add(chatDate);
            chatRecord.setChatDate(chatDates);
        }
        chatRecordDao.updateInsert(query,chatRecord);

    }

    @Override
    public List<ChatContent> selectRecord(Integer userId0, Integer userId1,String dateTime) {
        String id = userId0.toString() + "and" + userId1.toString();
        if (userId0 > userId1) {
            id = userId1.toString() + "and" + userId0;
        }
        id = MD5Util.MD5(id);

        ChatRecord record = chatRecordDao.queryOne(new ChatRecord(id));
        if (record != null) {
            List<ChatDate> dates= record.getChatDate();
            for (ChatDate date:dates){
                if(DateUtil.getStrDate(date.getRecordDate()).equals(dateTime)){
                    return date.getChatContent();
                }
            }
        }
        return null;
    }
}
